/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_kernel.htm
 *
 * Zhiqim Kernel is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.kernel.model.lists;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.zhiqim.kernel.constants.SignConstants;

/**
 * 根据ConcurrentHashMap创建
 *
 * @version v1.5.2 @author zouzhigang 2019-6-17 新建与整理
 */
public class ConcurrentSet<E> extends AbstractSet<E> implements Set<E>, SignConstants
{
    private final ConcurrentHashMap<E, String> map;

    public ConcurrentSet()
    {
        map = new ConcurrentHashMap<>();
    }
    
    public ConcurrentSet(int initialCapacity)
    {
        map = new ConcurrentHashMap<E, String>(initialCapacity);
    }
    
    @Override
    public Iterator<E> iterator()
    {
        return map.keySet().iterator();
    }

    @Override
    public int size()
    {
        return map.size();
    }
    
    public boolean isEmpty()
    {
        return map.isEmpty();
    }

    public boolean contains(Object o)
    {
        return map.containsKey(o);
    }

    public boolean add(E e)
    {
        return map.put(e, _EMPTY_) == null;
    }

    public boolean remove(Object o)
    {
        return map.remove(o) == _EMPTY_;
    }

    public void clear()
    {
        map.clear();
    }
}
